package com.xpgk.mvc.bentryRegister;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.storeconfig.ManagerSF;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.ext.directive.Str;
import com.jfinal.upload.UploadFile;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bchargeDebt.CollectArrears;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.binShopService.CustomerServiceMonthNurse;
import com.xpgk.mvc.binShopService.CustomerServiceNurse;
import com.xpgk.mvc.binShopService.ServicePartSize;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.bloseWeight.DelWeiTargetSum;
import com.xpgk.mvc.btoShop.BtoShop;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.leaveApplication.LeaveApplication;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSales.ProductSalesOrder;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.productStock.ProductStock;
import com.xpgk.mvc.productStock.ProductStockInfo;
import com.xpgk.mvc.scallVisit.ScallVisit;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.mvc.statistic.monthTotalStatistic.MonthTotal;
import com.xpgk.mvc.statistic.yearTotalStatistic.YearTotal;
import com.xpgk.plugin.FileRenamePlugin;
import com.xpgk.tools.ToolDateTime;
import com.xpgk.tools.ToolRandoms;

import net.sf.json.JSONObject;

@Service(name = BentryRegisterService.serviceName)
public class BentryRegisterService extends BaseService {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BentryRegisterService.class);
	public static final String serviceName = "bentryRegisterService";

	/**
	 * 计算消费金额
	 * 
	 * @param delWeight
	 * @param productIds
	 * @return
	 */
	public RenderBean getCost(String tem, String productIds, String orgids) {
		double cost = 0;
		double tream = 1;
		String msg = "";
		RenderBean result = new RenderBean();
		Product p = Product.dao.findById(productIds);
		String dlcsql = getSqlMy(Product.sqlId_selProByIds);
		Product dlc = Product.dao.findFirst(dlcsql, productIds, orgids);
		if (dlc != null) {
			// 如果是单疗程
			if (p != null && p.getStr(Product.column_proUnits).equals(Code.dlc_units)) {
				// 查出产品价格
				// cost=680;

				if (dlc.getFloat(ProductPrice.column_outPrice) != null) {
					cost = dlc.getFloat(ProductPrice.column_outPrice);
				} else {
					msg = "疗程未设置价格！！";
				}
			}
			// 如果是一疗程
			else if (p != null && p.getStr(Product.column_proUnits).equals(Code.bzlc_units)
					&& p.getStr(Product.column_proCapacity).equals(Code.ylc_capacity)) {
				if (dlc.getFloat(ProductPrice.column_outPrice) != null) {
					cost = dlc.getFloat(ProductPrice.column_outPrice);
				} else {
					msg = "疗程未设置价格！！";
				}
			} else {
				if (!tem.equals("")) {

					// 查出产品价格
					cost = dlc.getFloat(ProductPrice.column_outPrice) * Integer.parseInt(tem);
					tream = Integer.parseInt(tem);
				} else {

					if (dlc.getFloat(ProductPrice.column_outPrice) != null) {
						cost = dlc.getFloat(ProductPrice.column_outPrice);
					} else {
						msg = "疗程未设置价格！！";
					}
				}
			}
		} else {
			msg = "疗程未设置价格！！";
		}

		result.setMsg(msg);
		result.getMap().put("cost", cost);
		result.getMap().put("tream", tream);
		return result;
	}

	/**
	 * 保存顾客登记信息
	 */
	public RenderBean saveCus(Customer cus, CustomerDetail cusDet, CustomerAgreement cusAgr, String orgids) {
		RenderBean result = new RenderBean();
		String msg = "";
		/*Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Customer.column_hybh);
		param.put("organIds",  Customer.column_organIds);*/
		String sql = "select * from sb_customer where hybh = ? and organIds = ? and  state=1   order  by  hybh  desc";
		Customer cc = Customer.dao.findFirst(sql, cus.getStr(Customer.column_hybh),orgids);
		if (cc != null) {
			cc.set(Customer.column_sfzmhm, cus.getStr(Customer.column_sfzmhm));
			if (!cc.update()) {
				log.debug("更新顾客信息失败");
				throw new RuntimeException("更新顾客信息失败!");
			}
			Map<String, Object> param1 = new HashMap<String, Object>();
			param1.put("column", CustomerDetail.column_customerIds);
			String sqldet = getSqlByBeetl(CustomerDetail.sqlId_column, param1);
			CustomerDetail cd = CustomerDetail.dao.findFirst(sqldet, cc.getStr(Customer.column_ids));
			if (cd != null) {
						cd.set(CustomerDetail.column_birth, cusDet.getDate(CustomerDetail.column_birth))
						.set(CustomerDetail.column_job, cusDet.getStr(CustomerDetail.column_job))
						.set(CustomerDetail.column_sex,  cc.getStr(Customer.column_sex))
						.set(CustomerDetail.column_age,  cc.getInt(Customer.column_age))
						.set(CustomerDetail.column_username,  cc.getStr(Customer.column_userName))
						.set(CustomerDetail.column_provice, cusDet.getStr(CustomerDetail.column_provice))
						.set(CustomerDetail.column_city, cusDet.getStr(CustomerDetail.column_city))
						.set(CustomerDetail.column_area, cusDet.getStr(CustomerDetail.column_area))
						.set(CustomerDetail.column_address, cusDet.getStr(CustomerDetail.column_address))
						.set(CustomerDetail.column_chargePersion, cusDet.getStr(CustomerDetail.column_chargePersion))
						.set(CustomerDetail.column_customerType, 0);
						if(cusAgr.getStr(CustomerAgreement.column_codeIds) !=null &&cusAgr.getStr(CustomerAgreement.column_codeIds).equals("0")){
							cd.set(CustomerDetail.column_customerType, 1);//只塑性
						}
				if (!cd.update()) {
					log.debug("更新顾客详细信息失败");
					throw new RuntimeException("更新顾客详细信息失败!");
				}

			}
			//保存日护理综合查询表（ 新顾客单疗程数，新顾客疗程数）
			DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(orgids,new Date());
			if(dnt != null)
			{
				if(cusAgr.getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dnt.set(DailyNursingTotal.column_xgkDlcs, dnt.getInt(DailyNursingTotal.column_xgkDlcs)+cusAgr.getInt(CustomerAgreement.column_treatment));
				}else
				{
					dnt.set(DailyNursingTotal.column_xgkLcs, dnt.getInt(DailyNursingTotal.column_xgkLcs)+cusAgr.getInt(CustomerAgreement.column_treatment));
				}
				if (!dnt.update()) {
					log.debug("更新日护理综合查询表失败");
					throw new RuntimeException("更新日护理综合查询表失败!");
				}
			}else{
				DailyNursingTotal  dntNew =new  DailyNursingTotal();
				dntNew.set(DailyNursingTotal.column_organids, orgids);
				if(cusAgr.getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntNew.set(DailyNursingTotal.column_xgkDlcs, cusAgr.getInt(CustomerAgreement.column_treatment));
				}else
				{
					dntNew.set(DailyNursingTotal.column_xgkLcs, cusAgr.getInt(CustomerAgreement.column_treatment));
				}
				if (!dntNew.save()) {
					log.debug("更新日护理综合查询表失败");
					throw new RuntimeException("更新日护理综合查询表失败!");
				}
			}
			
		
			msg = ConstantRender.render_success_code;
			result.getMap().put("customerIds", cc.getStr(Customer.column_ids));
		} else {
			log.debug("根据会员编号没有查出顾客信息！顾客不存在！！");
			msg = "顾客不存在！";
		}
		result.getMap().put("msg", msg);
		return result;

	}

	/**
	 * 获取协议编号
	 * @param date 
	 * 
	 * @param tre(疗程数)
	 * @return 编号
	 */
	public static String getBH(String depBH, Integer treatment,String hybh, Date date1) {
		// 获取当月
		SimpleDateFormat df = new SimpleDateFormat("yyMM");
		String date = "";
		if(date1==null) {
			date = df.format(new Date());
		}else {
			date = df.format(date1);
		}
		// //获取当前代理商门店编号
		// String dlmdBH="1401001";
		String chybh = hybh.substring(2, hybh.length());
		String p =  depBH + date + String.format("%02d", treatment) + chybh;
		return p;



	}

	/**
	 * 上传正面照和侧面照
	 * 
	 * @param files
	 * @param cusIds
	 * @return
	 */
	public JSONObject imageUpload(List<UploadFile> files, String cusIds) {
		try {
			JSONObject json = new JSONObject();
			Map<String, String> nameMap = new HashMap<>();
			if (files != null && files.size() > 0) {
				for (int i = 0; i < files.size(); i++) {
					String uploadPath = files.get(i).getUploadPath();// 获取保存文件的文件夹
					//System.out.println(uploadPath);
					String fileName = files.get(i).getFileName();// 获取保存文件的文件名
					//String fileName =  cusIds+fileName1.substring(fileName1.lastIndexOf("."));
					String filePath = uploadPath + "/" + fileName;// 保存文件的路径
					System.out.println(filePath);
					String extension = fileName.substring(fileName.lastIndexOf("."));
					String fName = fileName.substring(0, fileName.lastIndexOf("."));
					String resultFileName = FileRenamePlugin.instance.changeFileName(uploadPath + "/" + fName, extension);// 为了避免相同文件上传冲突，使用工具类重新命名
					nameMap.put(files.get(i).getParameterName(), resultFileName);
					File source = new File(filePath);
					String savePath = Code.savePicture_path;
					if (".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension)
							|| "jpeg".equals(extension) || "bmp".equals(extension)) {
						try {
							FileInputStream fis = new FileInputStream(source);

							File targetDir = new File(savePath);
							if (!targetDir.exists()) {
								targetDir.mkdirs();
							}
							String fileName1 =  cusIds+fileName.substring(fileName.lastIndexOf("."));
							File target = new File(targetDir, fileName1);
							if (!target.exists()) {
								target.createNewFile();
							}
							FileOutputStream fos = new FileOutputStream(target);
							byte[] bts = new byte[1024 * 20];
							while (fis.read(bts, 0, 1024 * 20) != -1) {
								fos.write(bts, 0, 1024 * 20);
							}
							fos.close();
							fis.close();
							json.put("status", 0);
							source.delete();

						} catch (FileNotFoundException e) {
							e.printStackTrace();
							json.put("status", 1);
							json.put("message", "上传出现错误，请稍后再上传");
						} catch (IOException e) {
							e.printStackTrace();
							json.put("status", 1);
							json.put("message", "文件写入服务器出现错误，请稍后再上传");
						}
					} else {
						source.delete();
						json.put("status", 1);
						json.put("message", "只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
					}
				}
			} else {
				json.put("status", 2);
				json.put("message", "顾客信息保存成功，未上传图片");
			}
			Customer cus = Customer.dao.findById(cusIds);
			if (cus != null) {
				cus.set(Customer.column_zmPicture, nameMap.get("zmfile"))
						.set(Customer.column_cmPicture, nameMap.get("cmfile")).update();
			}
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		
		
		
	}

	/**
	 * 根据顾客（会员）编号，手机号，姓名查出顾客信息
	 */
	public Map<String, Object> getCusByHybh(String hybh, String name, String phone, String orgid) {

		String msg = ConstantRender.render_success_code;
		if (hybh != null && !hybh.equals("")) {
			hybh = String.format("%06d", Integer.parseInt(hybh));
		}

		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("organIds", orgid);
		queryParam.put("hybh", hybh);
		queryParam.put("name", name);
		queryParam.put("phone", phone);

		List<Record> lis = find(Customer.sqlId_selCus, queryParam);
		Map<String, Object> mm = new HashMap<>();
		if (lis != null && lis.size() > 0) {
			if (lis.size() > 1) {
				msg = "姓名重复，请用手机号或编号查询!";
				mm.put("customer", null);
			} else {
				Customer  cus=Customer.dao.findById(lis.get(0).get(Customer.column_ids));
				
				String sql1 = " select  *  from sb_customer_service_partsize   where customerIds= ?  ORDER BY  createDate  DESC ";
				List<Record> sps = Db.find(sql1 , cus.get("ids"));
				Date gq= null;
				long days = -1;//第一次量体重
				if(sps!=null&&sps.size()>0){
					Record ps = sps.get(0);
					gq=ps.getDate("createDate");
					long diff = new Date().getTime() - gq.getTime();//这样得到的差值是微秒级别  
				    days = diff / (1000 * 60 * 60 * 24);  
				}
				long count = 15;
				if(days>=count||days==-1){
					mm.put("daysflag", "1");
					mm.put("days", days);
				}
				String sql2 = " select  *  from sb_customer_detail   where customerIds= ?   ";
				List<Record>  cdl = Db.find(sql2 , cus.get("ids"));
				Record r = cdl.get(0);
				SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");
				r.set("toStoreDate",sdf.format(r.getDate("toStoreDate")) );
				r.set("minDate",sdf.format(r.getDate("minDate")) );
				mm.put("customerDetail", r);
				mm.put("customer", cus);
			}
		} else {
			msg = "会员不存在，请重新输入查询条件！";
		}
		mm.put("msg", msg);
		return mm;
	}

	
	
	/**
	 * 根据顾客（会员）编号，手机号，姓名查出顾客信息
	 */
	public Map<String, Object> getCusSizeByHybh(String hybh, String name, String phone, String orgid,String date) {

		String msg = ConstantRender.render_success_code;
		if (hybh != null && !hybh.equals("")) {
			hybh = String.format("%06d", Integer.parseInt(hybh));
		}

		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("organIds", orgid);
		queryParam.put("hybh", hybh);
		queryParam.put("name", name);
		queryParam.put("phone", phone);

		List<Record> lis = find(Customer.sqlId_selCus, queryParam);
		Map<String, Object> mm = new HashMap<>();
		if (lis != null && lis.size() > 0) {
			if (lis.size() > 1) {
				msg = "姓名重复，请用手机号或编号查询!";
				mm.put("customer", null);
			} else {
				Customer  cus=Customer.dao.findById(lis.get(0).get(Customer.column_ids));
				
				String sql1 = " select  *  from sb_customer_service_partsize   where customerIds= ?  ORDER BY  createDate  DESC ";
				List<Record> sps = Db.find(sql1 , cus.get("ids"));
				Date gq= null;
				
				
				String sql2 = " select  *  from sb_customer_detail   where customerIds= ?   ";
				List<Record>  cdl = Db.find(sql2 , cus.get("ids"));
				Record r = cdl.get(0);
				SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");
				r.set("toStoreDate",sdf.format(r.getDate("toStoreDate")) );
				r.set("minDate",sdf.format(r.getDate("minDate")) );
				mm.put("customerDetail", r);
				mm.put("customer", cus);
				
				
				String sqlSize = "SELECT * FROM sb_customer_service_partsize where customerIds = ? and DATE_FORMAT(createDate, '%Y-%m-%d')  = ? and dataStatus in ('1','2')";
				ServicePartSize servicePartSize = ServicePartSize.dao.findFirst(sqlSize,cus.getStr(Customer.column_ids),date);
				mm.put("servicePartSize", servicePartSize);
			}
		} else {
			msg = "会员不存在，请重新输入查询条件！";
		}
		mm.put("msg", msg);
		return mm;
	}

	// 根据机构id获取机构编号
	public static String getOrgBh(String orgIds) {
	Organization or = Organization.dao.findById(orgIds);
	if (or != null) {
	if (or.getStr(Organization.column_xzqh) != null) {
	String mdbh = String.format("%02d", or.getInt(Organization.column_mdbh));
	return or.getStr(Organization.column_xzqh) + mdbh;
	} else {
	return null;
	}

	} else {
	return null;
	}

	}



	/**
	 * 上一个下一个
	 * 
	 * @param hybh
	 * @param obj
	 * @param orgIds
	 * @return
	 */
	public Customer selOther(String hybh, String obj, String orgIds) {
		Customer cus = new Customer();
		if (hybh != "") {
			int n = Integer.parseInt(hybh);
			if (obj != "") {
				if (obj.equals("1")) {
					log.debug("获取上一条数据");
					hybh = String.format("%06d", n - 1);
				}
				if (obj.equals("2")) {
					log.debug("获取下一条数据");
					hybh = String.format("%06d", n + 1);
				}
			}
			String sql = getSqlMy(Customer.sqlId_selCusByHybhAndOrg);
			Customer cc = Customer.dao.findFirst(sql, hybh, orgIds);
			if (cc != null) {
				cus = cc;
			}else
			{
				cus=null;
			}
		} else {
			 if(obj.equals("1"))
			 {
				log.debug("获取上一条数据");
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("column", Customer.column_organIds);
				String sql1 = getSqlByBeetl(Customer.sqlId_column, param1);
				List<Customer> ccList = Customer.dao.find(sql1, orgIds);
				if (ccList != null && ccList.size() > 0) {
					cus = ccList.get(0);
				}
			 }else
			 {
					cus=null;
			 }
		}
		return cus;
	}


	/**
	 * 保存基础资料
	 * 
	 * @param controller
	 * @return
	 */
	public RenderBean saveDailyLife(CustomerHabit customerHabit,String organIds,String userIds) {
		RenderBean result = new RenderBean();
		String  msg="";
		Map<String, Object> param = new HashMap<String, Object>();
		String cusIds = customerHabit.getStr(CustomerHabit.column_customerID);
		if(cusIds == null){
			msg="请选择顾客";
			result.getMap().put("msg", msg);
			return result;
		}else{
	
			 if(userIds == null){
				msg = "操作人员不能为空，请重新登录";
				result.getMap().put("msg", msg);
				return result;
			}
				else{
				customerHabit.set(CustomerHabit.column_organIds, organIds);
				customerHabit.set(CustomerHabit.column_serviceID, userIds);
				customerHabit.set(CustomerHabit.column_createDate, ToolDateTime.getDate());
				if(customerHabit.save()){
					
					System.out.println("添加成功");
					msg=ConstantRender.render_success_code;
				
				}else{
					System.out.println("shibai ");
					msg="添加失败！";
				}
				
				result.getMap().put("msg", msg);
				return result;
		}
		
		}
		}
	/**
	 * 保存基础资料
	 * 
	 * @param controller
	 * @return
	 */
	public RenderBean saveBaseInfo(CustomerHabit customerHabit,String organIds,String userIds) {
		RenderBean result = new RenderBean();
		String  msg="";
		Map<String, Object> param = new HashMap<String, Object>();
		String cusIds = customerHabit.getStr(CustomerHabit.column_customerID);
		if(cusIds == null){
			msg="请选择顾客";
			result.getMap().put("msg", msg);
			return result;
		}else{
		param.put("column", customerHabit.column_customerID);
		String sql = getSqlByBeetl(customerHabit.sqlId_column, param);
		CustomerHabit habit = CustomerHabit.dao.findFirst(sql,customerHabit.getStr(CustomerHabit.column_customerID));
		if (habit!=null) {
			habit.set(CustomerHabit.column_organIds, organIds);
			habit.set(CustomerHabit.column_updateId, userIds);
			
			habit.set(CustomerHabit.column_gxsj, ToolDateTime.getDate());
			if(habit.update()){
				msg=ConstantRender.render_success_code;
			}else{
				msg="修改失败";
			}
			result.getMap().put("msg", msg);
			return result;
		}else{
			 if(userIds == null){
				msg = "操作人员不能为空，请重新登录";
				result.getMap().put("msg", msg);
				return result;
			}
				else{
				customerHabit.set(CustomerHabit.column_organIds, organIds);
				customerHabit.set(CustomerHabit.column_serviceID, userIds);
				customerHabit.set(CustomerHabit.column_createDate, ToolDateTime.getDate());
				if(customerHabit.save()){
					
					System.out.println("添加成功");
					msg=ConstantRender.render_success_code;
				
				}else{
					System.out.println("shibai ");
					msg="添加失败！";
				}
				
				result.getMap().put("msg", msg);
				return result;
		}
		}
		}
		}
	
	/**
	 * 修改资料
	 * 
	 * @param controller
	 * @return
	 */
	public JSONObject updateInfo(Customer customer,CustomerDetail customerDetail,String organIds,String userIds) {
		JSONObject json = new JSONObject();
		json.put("msg", "修改成功");
		String customerIds = customer.getStr(Customer.column_ids);
		int flag = 1;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", CustomerDetail.column_customerIds); 
		String sql = getSqlByBeetl(CustomerDetail.sqlId_column, param);
		CustomerDetail oldCustomerDetail = CustomerDetail.dao.findFirst(sql,customerIds);
//		
//		Map<String, Object> param2 = new HashMap<String, Object>();
//		param2.put("column",CustomerAgreement.column_customerID); 
//		String sql2 = getSqlByBeetl(CustomerAgreement.sqlId_column, param);
//		//IntoPersonnel personnel = IntoPersonnel.dao.findFirst(sql, intoPersonnel.getStr(IntoPersonnel.column_userIds));
//		CustomerAgreement customerAgreement = CustomerAgreement.dao.findFirst(sql,customerIds);
		
		//验证手机号和姓名
		String phone = customer.getStr(Customer.column_phone);//用户修改的手机号
		String name = customer.getStr(Customer.column_name);//用户修改的姓名
		
			if(name!=null&&!name.equals("")){
				String sql1 = "select * from sb_customer WHERE name = ? and ids!=? and organIds = ? and dataStatus in (1,2)";
				List<Customer> cbis = Customer.dao.find(sql1, name,customerIds,organIds);
				if(cbis==null||cbis.size()==0){
					oldCustomerDetail.set(CustomerDetail.column_name, name);
				}else{
					json.put("msg", "姓名已经存在，请重新输入");
					//msg = "姓名已经存在，请重新输入";
					flag = 0;
					return json;
				}
				
			}else{
				json.put("msg", "请检查姓名");
				flag = 0;
				return json;
				
			}
		
	
		if(phone!=null&&!phone.equals("")&&phone.length()==11){
			String sql1 = "select * from sb_customer WHERE phone = ? and ids!=?  and organIds = ? and dataStatus in (1,2)";
			List<Customer> cbis = Customer.dao.find(sql1, phone,customerIds,organIds);
			if(cbis==null||cbis.size()==0){
				oldCustomerDetail.set(CustomerDetail.column_phone, phone);
				oldCustomerDetail.set(CustomerDetail.column_username, phone);
			}else{
				json.put("msg", "手机号已经存在，请重新输入");
				flag = 0;
				return json;
			
			}
			
		
		}else{
			json.put("msg", "请检查手机号");
			flag = 0;
			return json;
			
	}
		
		if(customer.getStr(Customer.column_sfzmhm)!=null){
			if(customer.getStr(Customer.column_sfzmhm).length()==18){
				oldCustomerDetail.set(CustomerDetail.column_sfzmhm,customer.getStr(Customer.column_sfzmhm));
			}else{
				json.put("msg", "请检查身份证号");
				flag = 0;
				return json;
				
			}
			
		}
		customer.set(Customer.column_updateDate, new Date()).set(Customer.column_updateId, userIds);
		if(!customer.update()){
			json.put("msg", "用户信息更新失败");
			flag = 0;
			return json;
		
		}else{
			if(flag == 1){
			if(customerDetail.getDate(CustomerDetail.column_birth)!=null){
				oldCustomerDetail.set(CustomerDetail.column_birth,customerDetail.getDate(CustomerDetail.column_birth));
			}
			if(customerDetail.getStr(CustomerDetail.column_job)!=null){
				oldCustomerDetail.set(CustomerDetail.column_job,customerDetail.getStr(CustomerDetail.column_job));
			}
			if(customerDetail.getStr(CustomerDetail.column_chargePersion)!=null){
				oldCustomerDetail.set(CustomerDetail.column_chargePersion,customerDetail.getStr(CustomerDetail.column_chargePersion));
			}
			if(customerDetail.getStr(CustomerDetail.column_chargePersion)!=null){
				oldCustomerDetail.set(CustomerDetail.column_chargePersion,customerDetail.getStr(CustomerDetail.column_chargePersion));
			}
			if(customerDetail.getStr(CustomerDetail.column_provice)!=null){
				oldCustomerDetail.set(CustomerDetail.column_provice,customerDetail.getStr(CustomerDetail.column_provice));
			}
			if(customerDetail.getStr(CustomerDetail.column_city)!=null){
				oldCustomerDetail.set(CustomerDetail.column_city,customerDetail.getStr(CustomerDetail.column_city));
			}
			if(customerDetail.getStr(CustomerDetail.column_area)!=null){
				oldCustomerDetail.set(CustomerDetail.column_area,customerDetail.getStr(CustomerDetail.column_area));
			}
			if(customerDetail.getStr(CustomerDetail.column_address)!=null){
				oldCustomerDetail.set(CustomerDetail.column_address,customerDetail.getStr(CustomerDetail.column_address));
			}
			if(customerDetail.getStr(CustomerDetail.column_productNo)!=null){
				String sql1 = "SELECT * from sb_customer_detail where organIds = ? and customerIds!=? and productNo = ?  and dataStatus in (1,2)";
				List<CustomerDetail> cbis = CustomerDetail.dao.find(sql1, organIds,customerIds,customerDetail.getStr(CustomerDetail.column_productNo));
				if(cbis==null||cbis.size()==0){
					String sqlPro = "SELECT * from sb_customer_product where customerID = ? and organids = ? and dataStatus in (1,2)";
					List<CustomerProduct> customerProducts = CustomerProduct.dao.find(sqlPro,customerIds,organIds);
					if(customerProducts!=null && customerProducts.size()>0){
						for(int i = 0;i<customerProducts.size();i++){
							if(customerProducts.get(i).getStr(CustomerProduct.column_productID).equals("95e2ae17462e4d0eb1dc84bc63bc478a")||customerProducts.get(i).getStr(CustomerProduct.column_productID).equals("daaba791f4104ae98504a1bc421e0d8d")){
								customerProducts.get(i).set(CustomerProduct.column_productNo, customerDetail.getStr(CustomerDetail.column_productNo))
								.set(CustomerProduct.column_updateDate, new Date())
								.set(CustomerProduct.column_updateId, userIds);
								if(!customerProducts.get(i).update()){
									json.put("msg", "更新顾客产品编号失败");
									flag = 0;
									return json;
								}
							}
							
						}
					}
				}else{
					json.put("msg", "产品编号已经存在，请重新输入");
					flag = 0;
					return json;
				
				}
				
				
				
				
			
				oldCustomerDetail.set(CustomerDetail.column_productNo,customerDetail.getStr(CustomerDetail.column_productNo));
			}
//			if(customer.getStr(Customer.column_registtime)!=null){
//				oldCustomerDetail.set(CustomerDetail.column_firstDate,customer.getDate(Customer.column_registtime));
//				if(oldCustomerDetail.getFloat(CustomerDetail.column_firstWeight)==oldCustomerDetail.getFloat(CustomerDetail.column_minWeight)){
//					oldCustomerDetail.set(CustomerDetail.column_minDate,customer.getDate(Customer.column_registtime));
//				}
//				customerAgreement.set(CustomerAgreement.column_time, customer.getStr(Customer.column_registtime))
//				.set(CustomerAgreement.column_updateDate, new Date())
//				.set(CustomerAgreement.column_updateId, userIds);
//				
//			}
			
			if(customerDetail.getDate(CustomerDetail.column_firstDate)!=null){
				
				//保存日护理综合查询表
						
						
				DailyNursingTotal  oldDnt = DailyNursingTotal.dao.getDnt(organIds,oldCustomerDetail.getDate(CustomerDetail.column_firstDate));
						if(oldDnt != null)
						{
							oldDnt.set(DailyNursingTotal.column_cjrs, oldDnt.getInt(DailyNursingTotal.column_cjrs)-1);
							if(oldCustomerDetail.getStr(CustomerDetail.column_type)!=null){
							if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.zjs_ids))//如果是转介绍
							{
								oldDnt.set(DailyNursingTotal.column_zjscjrs, oldDnt.getInt(DailyNursingTotal.column_zjscjrs)-1);
							}else if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.xc_ids))
							{
								oldDnt.set(DailyNursingTotal.column_xccjrs, oldDnt.getInt(DailyNursingTotal.column_xccjrs)-1);
							}else
							{
								oldDnt.set(DailyNursingTotal.column_zrjdcjrs, oldDnt.getInt(DailyNursingTotal.column_zrjdcjrs)-1);
							}
							}
							if (!oldDnt.update()) {
								log.debug("更新日护理综合查询失败！！");
								throw new RuntimeException("更新日护理综合查询失败!");
							}	
						}
						
						DailyNursingTotal  newDnt = DailyNursingTotal.dao.getDnt(organIds,customerDetail.getDate(CustomerDetail.column_firstDate));
						if(newDnt!=null){
							newDnt.set(DailyNursingTotal.column_cjrs, newDnt.getInt(DailyNursingTotal.column_cjrs)+1);
							if(oldCustomerDetail.getStr(CustomerDetail.column_type)!=null){
								if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.zjs_ids))//如果是转介绍
								{
									newDnt.set(DailyNursingTotal.column_zjscjrs, newDnt.getInt(DailyNursingTotal.column_zjscjrs)+1);
								}else if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.xc_ids))
								{
									newDnt.set(DailyNursingTotal.column_xccjrs, newDnt.getInt(DailyNursingTotal.column_xccjrs)+1);
								}else
								{
									newDnt.set(DailyNursingTotal.column_zrjdcjrs, newDnt.getInt(DailyNursingTotal.column_zrjdcjrs)+1);
								}
							}
							
							if (!newDnt.update()) {
								log.debug("更新日护理综合查询失败！！");
								throw new RuntimeException("更新日护理综合查询失败!");
							}	
						}else{
							newDnt = new DailyNursingTotal();
							newDnt.set(DailyNursingTotal.column_organids, organIds)
							.set(DailyNursingTotal.column_date, ToolDateTime.format(customerDetail.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd))
							.set(DailyNursingTotal.column_zxrs, 1)
							.set(DailyNursingTotal.column_cjrs, 1);
							if(oldCustomerDetail.getStr(CustomerDetail.column_type)!=null){
								if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.zjs_ids))//如果是转介绍
								{
									newDnt.set(DailyNursingTotal.column_zjscjrs,1);
								}else if(oldCustomerDetail.getStr(CustomerDetail.column_type).equals(Code.xc_ids))
								{
									newDnt.set(DailyNursingTotal.column_xccjrs, 1);
								}else
								{
									newDnt.set(DailyNursingTotal.column_zrjdcjrs, 1);
								}
							}
							
						if (!newDnt.save()) {
							log.debug("保存日护理综合查询（新顾客咨询人数失败）！！");
							throw new RuntimeException("保存日护理综合查询（新顾客咨询人数失败）!");
						}
						}
				
				
				oldCustomerDetail.set(CustomerDetail.column_firstDate,customerDetail.getDate(CustomerDetail.column_firstDate));
				//修改总目标开始、结束时间
				String sqlTarget = "SELECT * FROM sb_customer_dtarget where customerIds = ? and targetName = '总目标' and dataStatus in (1,2)";
				DelWeiTargetSum delWeiTargetSum = DelWeiTargetSum.dao.findFirst(sqlTarget,customerIds);
				Date date = customerDetail.getDate(CustomerDetail.column_firstDate);//首次进店日期
				String lscStr = oldCustomerDetail.getStr(CustomerDetail.column_sjtreatment);//购买疗程数
				int lcs =0;
				if(lscStr!=null && !lscStr.equals("")){
					lcs = Integer.parseInt(lscStr);
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				cal.add(cal.MONTH, lcs);
				SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String lastMonth = dft.format(cal.getTime());
				delWeiTargetSum.set(DelWeiTargetSum.column_startTime, customerDetail.getDate(CustomerDetail.column_firstDate))
				.set(DelWeiTargetSum.column_endTime, lastMonth)
				.set(DelWeiTargetSum.column_updateDate,new Date())
				.set(DelWeiTargetSum.column_updateId, userIds);
				if(!delWeiTargetSum.update()){
					json.put("msg", "更新顾客总目标失败");
					flag = 0;
					return json;
				}
				
				//修改协议签订时间
				
				String sqlAgree = "SELECT * from sb_customer_agreement where customerIds = ? and organIds = ? and dataStatus in (1,2)";
				CustomerAgreement customerAgreement = CustomerAgreement.dao.findFirst(sqlAgree,customerIds,organIds);
				if(customerAgreement!=null){
					customerAgreement.set(CustomerAgreement.column_time, customerDetail.getDate(CustomerDetail.column_firstDate))
					.set(CustomerAgreement.column_updateDate,new Date())
					.set(CustomerAgreement.column_updateId, userIds);
					if(!customerAgreement.update()){
						json.put("msg", "更新顾客协议签订时间失败");
						flag = 0;
						return json;
					}
				}
			}
			oldCustomerDetail.set(CustomerDetail.column_updateDate,new Date());
			oldCustomerDetail.set(CustomerDetail.column_updateId,userIds);
			if(!oldCustomerDetail.update()){
				json.put("msg", "更新顾客详细信息失败");
				flag = 0;
				return json;
				
			}
			
			}
			
			
		}
		return json;
		
		
		}
	
	public List<Customer> getCusInfo(String orgid) {
		String sql1 = "select ids,name,phone,hybh from sb_customer where organIds = ? and  dataStatus in (1,2)";
		List<Customer> cbis = Customer.dao.find(sql1, orgid);
		return cbis;
	}

	public List<Record> getView(String cUserOrganIds) {
		String sql1 = "SELECT b.name,a.* "
					+"FROM"
					+"sb_customer_service a"
					+"LEFT JOIN  sb_customer_detail b"
					+"ON a.customerID = b.customerIds"
					+" WHERE a.endTime IS NULL AND  a.dataStatus in ('1','2')  AND a.organids =?  ORDER BY  a.createDate DESC";
		List<Record> cbis = Db.find(sql1, cUserOrganIds);
		return cbis;
	}
	
	public String deleteCus(String customerIds,String userIds,String organids){
		String msg = "success";
		String customerID = "";
		String[] cusIdsArray = customerIds.split(",");
		for(int i=0;i<cusIdsArray.length;i++){
			customerID = cusIdsArray[i];
			String sqlcusService = "select * from sb_customer_service where customerID = ? and organids = ? and dataStatus in ('1','2')";
			List<CustomerService> cusService = CustomerService.dao.find(sqlcusService,customerID,organids);
			if(cusService!=null&&cusService.size()>0){
				msg = "顾客已经进行过到店服务，不能删除";
				return msg;
			}else{
			String sqlcus = "select * FROM sb_customer WHERE ids = ? and organIds = ? and dataStatus in(1,2)";
			List<Customer> cus = Customer.dao.find(sqlcus,customerID,organids);
			for(int m = 0;m<cus.size();m++){
				int cusType =  cus.get(m).getInt(Customer.column_dataStatus);//是不是补录顾客
				cus.get(m).set(Customer.column_dataStatus, "0");
				cus.get(m).set(Customer.column_state, "0");
				cus.get(m).set(Customer.column_updateDate, new Date());
				cus.get(m).set(Customer.column_updateId, userIds);
				if(!cus.get(m).update()){
					msg = "顾客信息删除失败";
					return msg;
				}else{
					String phone = cus.get(m).getStr(Customer.column_phone);
					
					String sqlcusTem = "select * from sb_customer_temporary sbt where sbt.phone = ? and sbt.organIds = ? and sbt.dataStatus in (1,2)";
					List<Temporary> cusTem = Temporary.dao.find(sqlcusTem,phone,organids);
					for(int j = 0;j<cusTem.size();j++){
						cusTem.get(j).set(Temporary.column_dataStatus, "0");
						cusTem.get(j).set(Temporary.column_updateDate, new Date());
						cusTem.get(j).set(Temporary.column_updateId, userIds);
						if(!cusTem.get(j).update()){
							msg = "顾客测算信息删除失败"; 
							return msg;
						}
					}
				
					String sqlcusScall = "select * FROM sb_customer_scallvisit where phone = ? and organIds = ? and dataStatus in(1,2)";
					List<ScallVisit> cusScall = ScallVisit.dao.find(sqlcusScall,phone,organids);
					for(int j = 0;j<cusScall.size();j++){
						cusScall.get(j).set(ScallVisit.column_dataStatus, "0");
						cusScall.get(j).set(ScallVisit.column_updateDate, new Date());
						cusScall.get(j).set(ScallVisit.column_updateId, userIds);
						if(!cusScall.get(j).update()){
							msg = "顾客回访信息删除失败"; 
							return msg;
						}
					}
					
					
					
					String sqlcusTarget = "SELECT * FROM sb_customer_dtarget WHERE customerIds = ? and dataStatus in(1,2)";
					List<DelWeiTargetSum> cusTarget = DelWeiTargetSum.dao.find(sqlcusTarget,customerID);
					for(int j = 0;j<cusTarget.size();j++){
						cusTarget.get(j).set(DelWeiTargetSum.column_dataStatus, "0");
						cusTarget.get(j).set(DelWeiTargetSum.column_updateDate, new Date());
						cusTarget.get(j).set(DelWeiTargetSum.column_updateId, userIds);
						if(!cusTarget.get(j).update()){
							msg = "顾客减重目标删除失败";
							return msg;
						}else{
							String tarIds = cusTarget.get(j).getStr(DelWeiTargetSum.column_ids);
							String sqlcusTargetDetail = "SELECT * from sb_customer_dtarget_detail where sc_targetIds = ? and dataStatus in (1,2)";
							List<DelWeiTarget> cusTargetDetail = DelWeiTarget.dao.find(sqlcusTargetDetail,tarIds);
							for(int n = 0;n<cusTargetDetail.size();n++){
								cusTargetDetail.get(n).set(DelWeiTarget.column_dataStatus, "0");
								cusTargetDetail.get(n).set(DelWeiTarget.column_updateDate, new Date());
								cusTargetDetail.get(n).set(DelWeiTarget.column_updateId, userIds);
								if(!cusTargetDetail.get(n).update()){
									msg = "顾客减重目标明细信息删除失败";
									return msg;
								}
							}
						}
					}
					
					
					String sqlcusAgree = "SELECT * FROM sb_customer_agreement where customerIds = ? and  organIds = ? and dataStatus in(1,2)";
					List<CustomerAgreement> cusAgree = CustomerAgreement.dao.find(sqlcusAgree,customerID,organids);
					for(int j = 0;j<cusAgree.size();j++){
						cusAgree.get(j).set(CustomerAgreement.column_dataStatus, "0");
						cusAgree.get(j).set(CustomerAgreement.column_state, "0");
						cusAgree.get(j).set(CustomerAgreement.column_updateDate, new Date());
						cusAgree.get(j).set(CustomerAgreement.column_updateId, userIds);
						if(!cusAgree.get(j).update()){
							msg = "顾客协议删除失败";
							return msg;
						}
					}
					
					String sqlcusDetail = "SELECT * from sb_customer_detail where customerIds = ? and organIds = ? and dataStatus in(1,2)";
					CustomerDetail cusDetail = CustomerDetail.dao.findFirst(sqlcusDetail,customerID,organids);
					DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(organids,cusDetail.getDate(CustomerDetail.column_firstDate));
					if(dnt!=null){
						if(dnt.getInt(DailyNursingTotal.column_cjrs)!=null&& dnt.getInt(DailyNursingTotal.column_cjrs)!=0){
							dnt.set(DailyNursingTotal.column_cjrs, dnt.getInt(DailyNursingTotal.column_cjrs)-1);
						}
						if(cusDetail.getStr(CustomerDetail.column_type)!=null){
							if(cusDetail.getStr(CustomerDetail.column_type).equals(Code.zjs_ids))//如果是转介绍
							{
								if(dnt.getInt(DailyNursingTotal.column_zjscjrs)!=null&& dnt.getInt(DailyNursingTotal.column_zjscjrs)!=0){
									dnt.set(DailyNursingTotal.column_zjscjrs, dnt.getInt(DailyNursingTotal.column_zjscjrs)-1);
								}
							}else if(cusDetail.getStr(CustomerDetail.column_type).equals(Code.xc_ids))
							{
								if(dnt.getInt(DailyNursingTotal.column_xccjrs)!=null&& dnt.getInt(DailyNursingTotal.column_xccjrs)!=0){
									dnt.set(DailyNursingTotal.column_xccjrs, dnt.getInt(DailyNursingTotal.column_xccjrs)-1);
								}
							}else
							{
								if(dnt.getInt(DailyNursingTotal.column_zrjdcjrs)!=null&& dnt.getInt(DailyNursingTotal.column_zrjdcjrs)!=0){
									dnt.set(DailyNursingTotal.column_zrjdcjrs, dnt.getInt(DailyNursingTotal.column_zrjdcjrs)-1);
								}
							}
						}
						
						dnt.update();
					}
					
						cusDetail.set(CustomerDetail.column_dataStatus, "0");
						cusDetail.set(CustomerDetail.column_updateDate, new Date());
						cusDetail.set(CustomerDetail.column_updateId, userIds);
						if(!cusDetail.update()){
							msg = "顾客详细信息删除失败";
							return msg;
						}
					
					
					
					
					String sqlcusHabit = "select * from sb_customer_habit where customerID = ?  and organIds = ? and dataStatus in (1,2)";
					List<CustomerHabit> cusHabit = CustomerHabit.dao.find(sqlcusHabit,customerID,organids);
					for(int j = 0;j<cusHabit.size();j++){
						cusHabit.get(j).set(CustomerHabit.column_dataStatus, "0");
						cusHabit.get(j).set(CustomerHabit.column_updateDate, new Date());
						cusHabit.get(j).set(CustomerHabit.column_updateId, userIds);
						if(!cusHabit.get(j).update()){
							msg = "顾客日常及基础资料删除失败";
							return msg;
						}
					}
					
					
					String sqlcusLeave = "SELECT * FROM sb_customer_leave scl where scl.costomerIds = ? and organIds = ? and dataStatus in (1,2)";
					List<LeaveApplication> cusLeave = LeaveApplication.dao.find(sqlcusLeave,customerID,organids);
					for(int j = 0;j<cusLeave.size();j++){
						cusLeave.get(j).set(LeaveApplication.column_dataStatus, "0");
						cusLeave.get(j).set(LeaveApplication.column_updateDate, new Date());
						cusLeave.get(j).set(LeaveApplication.column_updateId, userIds);
						if(!cusLeave.get(j).update()){
							msg = "顾客日常及基础资料删除失败";
							return msg;
						}
					}
					
					
					
					
					
					String sqlPro = "select * FROM sb_customer_product scp where scp.customerID = ? and scp.organids =? and scp.dataStatus in (1,2)";
					List<CustomerProduct> cuspro = CustomerProduct.dao.find(sqlPro,customerID,organids);
					for(int j = 0;j<cuspro.size();j++){
						String sqlTypeIds = "SELECT * FROM sb_product where ids = ?";
						Product product = Product.dao.findFirst(sqlTypeIds,cuspro.get(j).getStr(CustomerProduct.column_productID));
						cuspro.get(j).set(CustomerProduct.column_dataStatus, "0");
						cuspro.get(j).set(CustomerProduct.column_status, "0");
						cuspro.get(j).set(CustomerProduct.column_updateDate, new Date());
						cuspro.get(j).set(CustomerProduct.column_updateId, userIds);
						if(!cuspro.get(j).update()){
							msg = "顾客产品删除失败";
							return msg;
						}else{
							
							if(!product.getStr(Product.column_typeIds).equals("455018290a8f4afbbaf29c61904d6406")&&cusType==1){
								
							
							//把产品放回库存
							
							ProductOutin po = new ProductOutin();
							po.set(ProductOutin.column_number, cuspro.get(j).getInt(CustomerProduct.column_count))
							.set(ProductOutin.column_productIds, cuspro.get(j).getStr(CustomerProduct.column_productID))
							.set(ProductOutin.column_typeIds, product.getStr(Product.column_typeIds))
							.set(ProductOutin.column_organIds, organids)
							.set(ProductOutin.column_date, new Date())
							.set(ProductOutin.column_userIds,userIds)
							.set(ProductOutin.column_gxsj, new Date())
							.set(ProductOutin.column_style, "4")
							.set(ProductOutin.column_reason,"删除顾客")
							.set(ProductOutin.column_price, cuspro.get(j).getFloat(CustomerProduct.column_proUnitPrice));
							if(!po.save())
							{
								msg = "保存产品入库失败";
								return msg;
								
							}else{
								//更新产品库存表
								String  sptSql=getSqlMy(ProductStock.sqlId_selPSByOidsAndPids);
								ProductStock  ps=ProductStock.dao.findFirst(sptSql,organids,po.getStr(ProductOutin.column_productIds));
								if(ps!=null)
								{
									//更新
									float  num=ps.getFloat(ProductStock.column_num)+po.getInt(ProductOutin.column_number);
									ps.set(ProductStock.column_num, num)
										.set(ProductStock.column_gxsj, new Date());
									if(!ps.update())
									{
										msg = "更新产品总库存失败";
										return msg;
									}
									
								}else
								{
									ProductStock  psk=new ProductStock();
									psk.set(ProductStock.column_ids,ToolRandoms.getUuid(true))
										.set(ProductStock.column_organids, organids)
										.set(ProductStock.column_productID, po.getStr(ProductOutin.column_productIds))
										.set(ProductStock.column_num, (float)po.getInt(ProductOutin.column_number))
										.set(ProductStock.column_gxsj,new Date());
									if(!psk.save())
									{
										msg = "保存产品总库存失败";
										return msg;
									}
									ps=psk;
								}
								//更新库存明细
								SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
						    	String gxsj="%"+dateFormater.format(new Date())+"%";
								String  psiSql=getSqlMy(ProductStockInfo.sqlId_selByOidsAndPidsAndTime);
								ProductStockInfo  psi=ProductStockInfo.dao.findFirst(psiSql,organids,po.getStr(ProductOutin.column_productIds),gxsj);
								if(psi!=null)
						    	{	
						    		//库存详细信息减少库存
						    		float num=psi.getFloat(ProductStockInfo.column_stock)+po.getInt(ProductOutin.column_number);
						    		psi.set(ProductStockInfo.column_stock,num)
						    			.set(ProductStockInfo.column_gxsj, new Date());
						    		if(!psi.update())
						    		{
						    			msg= "更新库存明细失败";
						    			return msg;
						    		}
						    		
						    	}else
						    	{
						    		ProductStockInfo psInfo=new ProductStockInfo();
						    		String psinfoids = ToolRandoms.getUuid(true);
						    		psInfo.set(ProductStockInfo.column_ids, psinfoids)
						    				.set(ProductStockInfo.column_organids, organids)
						    				.set(ProductStockInfo.column_productID, po.getStr(ProductOutin.column_productIds))
						    				.set(ProductStockInfo.column_type,po.getStr(ProductOutin.column_typeIds))
						    				.set(ProductStockInfo.column_stock, ps.getFloat(ProductStock.column_num))
						    				.set(ProductStockInfo.column_gxsj, new Date());
						    		if(!psInfo.save())
							    	{
						    			msg = "保存产品库存记录失败！！";
						    			return msg;
							    	}	
						    	}
							}
						}
						}
					}
					
					
					String sqlProPlus = "select * FROM sb_customer_product_surplus scp where scp.customerIds = ? and scp.organIds =? and scp.dataStatus in (1,2)";
					List<CustomerProductSurplus> cusProSur = CustomerProductSurplus.dao.find(sqlProPlus,customerID,organids);
					
					for(int j = 0;j<cusProSur.size();j++){
						cusProSur.get(j).set(CustomerProductSurplus.column_dataStatus, "0");
						cusProSur.get(j).set(CustomerProductSurplus.column_updateDate, new Date());
						cusProSur.get(j).set(CustomerProductSurplus.column_updateId, userIds);
						if(!cusProSur.get(j).update()){
							msg = "顾客产品剩余删除失败";
							return msg;
						}
					}
					
					String sqlProOrder = "select * FROM sb_product_sales_order spso where  spso.customerIds = ? and spso.organIds = ? and spso.dataStatus in(1,2)";
					List<ProductSalesOrder> cusProOrder = ProductSalesOrder.dao.find(sqlProOrder,customerID,organids);
					for(int j = 0;j<cusProOrder.size();j++){
						cusProOrder.get(j).set(ProductSalesOrder.column_dataStatus, "0");
						cusProOrder.get(j).set(ProductSalesOrder.column_updateDate, new Date());
						cusProOrder.get(j).set(ProductSalesOrder.column_updateId, userIds);
						if(!cusProOrder.get(j).update()){
							msg = "顾客订单删除失败";
							return msg;
						}
					}
					
					
					
					String sqlProOrderDetail = "SELECT * FROM sb_product_sales_detail spsd WHERE spsd.customerID = ? and spsd.organids = ? and spsd.dataStatus in(1,2)";
					List<ProductSales> cusProOrderDetail = ProductSales.dao.find(sqlProOrderDetail,customerID,organids);
					for(int j = 0;j<cusProOrderDetail.size();j++){
						//更新日综合查询表
						DailyNursingTotal  dnt1 = DailyNursingTotal.dao.getDnt(organids,cusProOrderDetail.get(j).getDate(ProductSales.column_date));
						if(dnt1!=null ){
							String proIds = cusProOrderDetail.get(j).getStr(ProductSales.column_productID);
							Product p = Product.dao.findById(proIds);
							if(p!=null){
								dnt1.set(p.getStr(Product.column_proBh)+"SalNum", dnt1.getInt(p.getStr(Product.column_proBh)+"SalNum")-Integer.parseInt(cusProOrderDetail.get(j).get("number").toString()))
								.set(p.getStr(Product.column_proBh)+"SalPrice", dnt1.getDouble(p.getStr(Product.column_proBh)+"SalPrice")-Double.parseDouble(cusProOrderDetail.get(j).get("price").toString()))
								.set(DailyNursingTotal.column_priceTotal, dnt1.getDouble(DailyNursingTotal.column_priceTotal)-Double.parseDouble(cusProOrderDetail.get(j).get("price").toString()));
							if (p.getStr(Product.column_typeIds).equals(Code.lc_ids) || p.getStr(Product.column_typeIds).equals(Code.dlc_ids)) 
							{
								dnt1.set(DailyNursingTotal.column_jzzxse, dnt1.getDouble(DailyNursingTotal.column_jzzxse)-Double.parseDouble(cusProOrderDetail.get(j).get("price").toString()));
							}
							if (!dnt1.update()) {
								msg = "日护理表（销售）更新失败";
								return msg;
							}
							}
						}
						cusProOrderDetail.get(j).set(ProductSales.column_dataStatus, "0");
						cusProOrderDetail.get(j).set(ProductSales.column_updateDate, new Date());
						cusProOrderDetail.get(j).set(ProductSales.column_updateId, userIds);
						if(!cusProOrderDetail.get(j).update()){
							msg = "顾客订单详情删除失败";
							return msg;
						}
					}
					
					String sqlcollectarrears = "select * FROM sb_shop_collectarrears spc WHERE spc.customerIds = ? and spc.organIds = ? and dataStatus in(1,2)";
					List<CollectArrears> collectarrears = CollectArrears.dao.find(sqlcollectarrears,customerID,organids);
					for(int j = 0;j<collectarrears.size();j++){
						collectarrears.get(j).set(CollectArrears.column_dataStatus, "0");
						collectarrears.get(j).set(CollectArrears.column_updateDate,new Date());
						collectarrears.get(j).set(CollectArrears.column_updateId, userIds);
						if(!collectarrears.get(j).update()){
							msg = "顾客欠款删除失败";
							return msg;
						}
					}
					
					String sqlcusToShop = "select * from sb_customer_toshop sct where sct.customerIds = ? and sct.outShopIds = ? and sct.dataStatus in (1,2)";
					List<BtoShop> cusToShop = BtoShop.dao.find(sqlcusToShop,customerID,organids);
					for(int j = 0;j<cusToShop.size();j++){
						cusToShop.get(j).set(BtoShop.column_dataStatus, "0");
						cusToShop.get(j).set(BtoShop.column_updateDate, new Date());
						cusToShop.get(j).set(BtoShop.column_updateId, userIds);
						if(!cusToShop.get(j).update()){
							msg = "顾客转店信息删除失败";
							return msg;
						}
					}
					
					String sqlcusSerNurse = "select * from sb_customer_service_nurse where customerID = ? and organids = ? ";
					List<CustomerServiceNurse> cusSerNurse = CustomerServiceNurse.dao.find(sqlcusSerNurse,customerID,organids);
					for(int q = 0;q<cusSerNurse.size();q++){
						cusSerNurse.get(q).set(CustomerServiceNurse.column_dataStatus, "0");
						cusSerNurse.get(q).set(CustomerServiceNurse.column_updateDate, new Date());
						cusSerNurse.get(q).set(CustomerServiceNurse.column_updateId, userIds);
						if(!cusSerNurse.get(q).update()){
							msg = "顾客日护理删除失败";
						}
					}
					
					String sqlcusSerMonNurse = "SELECT * FROM sb_customer_service_month_nurse WHERE customerIds = ? and organIds = ? and dataStatus in ('1','2') ";
					List<CustomerServiceMonthNurse> cusSerMonNurse = CustomerServiceMonthNurse.dao.find(sqlcusSerMonNurse,customerID,organids);
					for(int q = 0;q<cusSerMonNurse.size();q++){
						cusSerMonNurse.get(q).set(CustomerServiceMonthNurse.column_dataStatus, "0");
						cusSerMonNurse.get(q).set(CustomerServiceMonthNurse.column_updateDate, new Date());
						cusSerMonNurse.get(q).set(CustomerServiceMonthNurse.column_updateId, userIds);
						if(!cusSerMonNurse.get(q).update()){
							msg = "顾客月护理删除失败";
						}
					}
//					String sqlcusService = "select * from sb_customer_service where customerID = ? and organids = ? and dataStatus in(1,2)";
//					List<CustomerService> cusService = CustomerService.dao.find(sqlcusService,customerID,organids);
//					for(int j = 0;j<cusService.size();j++){
//						cusService.get(j).set(CustomerService.column_dataStatus, "0");
//						cusService.get(j).set(CustomerService.column_updateDate, new Date());
//						cusService.get(j).set(CustomerService.column_updateId, userIds);
//						if(!cusService.get(j).update()){
//							msg = "顾客服务信息删除失败";
//						}else{
//							String sqlcusSerNurse = "select * from sb_customer_service_nurse where customerID = ? and organids = ? and dataStatus in (1,2)";
//							List<CustomerServiceNurse> cusSerNurse = CustomerServiceNurse.dao.find(sqlcusSerNurse,customerID,organids);
//							for(int q = 0;q<cusSerNurse.size();q++){
//								cusSerNurse.get(q).set(CustomerServiceNurse.column_dataStatus, "0");
//								cusSerNurse.get(q).set(CustomerServiceNurse.column_updateDate, new Date());
//								cusSerNurse.get(q).set(CustomerServiceNurse.column_updateId, userIds);
//								if(!cusSerNurse.get(q).update()){
//									msg = "顾客日护理删除失败";
//								}
//							
//							}
//							
//							String sqlcusSerPart = "select * from sb_customer_service_partsize where customerIds = ? and dataStatus in(1,2)";
//							List<ServicePartSize> cusSerPart = ServicePartSize.dao.find(sqlcusSerPart,customerID);
//							for(int q = 0;q<cusSerPart.size();q++){
//								cusSerPart.get(q).set(ServicePartSize.column_dataStatus, "0");
//								cusSerPart.get(q).set(ServicePartSize.column_updateDate, new Date());
//								cusSerPart.get(q).set(ServicePartSize.column_updateId, userIds);
//								if(!cusSerPart.get(q).update()){
//									msg = "顾客尺寸删除失败";
//								}
//							
//							}
//							
//							String sqlSerShaping = "select * FROM sb_customer_service_shaping where customerIds = ? and organIds = ? and dataStatus in (1,2)";
//							List<ServiceShaping> cusSerShaping = ServiceShaping.dao.find(sqlSerShaping,customerID);
//							for(int q = 0;q<cusSerShaping.size();q++){
//								cusSerShaping.get(q).set(ServiceShaping.column_dataStatus, "0");
//								cusSerShaping.get(q).set(ServiceShaping.column_updateDate, new Date());
//								cusSerShaping.get(q).set(ServiceShaping.column_updateId, userIds);
//								if(!cusSerShaping.get(q).update()){
//									msg = "顾客塑形删除失败";
//								}
//							
//							}
//							
//							
//						}
//					}
					
				}
			}
			}
		
			
			
			
			
			
			
//			 
		}

		return msg;
		
	}

	

}
